package blockchain.json;

import blockchain.block.Sha;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class BlockChainJson {
    public String productID = "null";                //产品ID
    public String batchNumber = "null";              //批次号
    public String productionProcess = "planting";    //生产环节
    public String productionLocation = "null";       //生产地点
    public String operatingTime = "2000";      //操作时间
    public String personInCharge = "null";           //负责人
    public String qualityInspector = "null";         //质检员
    public String qualityInspectorStatus = "qualified";   //质检状态
    public String temperature = "00";             //环境数据 温度
    public String humidity = "00";                //环境数据 湿度
    public BlockChainJson() {}
    public BlockChainJson(JsonObject json) {
        productID = json.get("productID").getAsString();
        batchNumber = json.get("batchNumber").getAsString();
        productionProcess = json.get("productionProcess").getAsString();
        productionLocation = json.get("productionLocation").getAsString();
        operatingTime = json.get("operatingTime").getAsString();
        personInCharge = json.get("personInCharge").getAsString();
        qualityInspector = json.get("qualityInspector").getAsString();
        qualityInspectorStatus = json.get("qualityInspectorStatus").getAsString();
        temperature = json.get("temperature").getAsString();
        humidity = json.get("humidity").getAsString();
    }
    public BlockChainJson(String jsonString) {
        this(new JsonParser().parse(jsonString).getAsJsonObject());
        JsonObject parser = new JsonParser().parse(jsonString).getAsJsonObject();;
        //JsonParser jsonParser = new JsonParser();
    }
    public JsonObject toJsonObject() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("productID", productID);
        jsonObject.addProperty("batchNumber", batchNumber);
        jsonObject.addProperty("productionProcess", productionProcess);
        jsonObject.addProperty("productionLocation", productionLocation);
        jsonObject.addProperty("operatingTime", operatingTime);
        jsonObject.addProperty("personInCharge", personInCharge);
        jsonObject.addProperty("qualityInspector", qualityInspector);
        jsonObject.addProperty("qualityInspectorStatus", qualityInspectorStatus);
        jsonObject.addProperty("temperature", temperature);
        jsonObject.addProperty("humidity", humidity);
        return jsonObject;
    }
    public String toJsonString() {
        return toJsonObject().toString();
    }
    public String getHash() {
        return Sha.Sha256(toJsonString());
    }
}
